package cn.sanshuilin.kst.mobile.client.controller;

import cn.sanshuilin.kst.common.upload.CosFileOperator;
import cn.sanshuilin.kst.mobile.client.constant.Message;
import cn.sanshuilin.kst.mobile.client.constant.ReturnCode;
import cn.sanshuilin.kst.system.api.service.ISubjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author greenlee
 * @version v1.0
 * @create 创建时间: 2018-02-23 11:05
 * Copyright 2018  by 三水林科技有限公司
 **/
@Validated //效验参数
@RestController //不用响应模版 html页
@RequestMapping("/{version}/service/subject/")
@Api(tags = "话题管理")
public class SubjectController {
    @Autowired
    private ISubjectService subjectService;

    @PostMapping(value = "savesubject1",produces = "application/json; charset=UTF-8")
    @ApiOperation(value = "上传")
    public Map<String, Object> saveSubject1(HttpServletRequest request){
        MultipartHttpServletRequest params=((MultipartHttpServletRequest) request);
        List<MultipartFile> files = ((MultipartHttpServletRequest) request)
                .getFiles("uploadFile");
        String address=params.getParameter("address");
        String token=params.getParameter("token");
        String lat=params.getParameter("lat");//纬度
        String lng=params.getParameter("lng");
        String description=params.getParameter("description");
        CosFileOperator cosFileOperator = new CosFileOperator();
        List<String> imageAttachmentIDs = cosFileOperator.saveFile(files);
        Map<String, Object> message = new HashMap<>();

//        subjectService.saveSubject(token,imageAttachmentIDs,lng,lat,address,description);
        message.put(Message.RETURN_FIELD_CODE, ReturnCode.SUCCESS);
//        message.put(Message.RETURN_FIELD_DATA, Collections.singletonMap("path", fileManager.getFileUrlByRealPath(path)));
        return message;
    }

    @PostMapping(value = "savesubject",produces = "application/json; charset=UTF-8")
    @ApiOperation(value = "保存话题")
    public Map<String, Object> saveSubject(
            @ApiParam(required = true, value = "版本", defaultValue = "v1") @PathVariable("version") String version,
            @ApiParam(required = true,value = "token")@RequestParam("token")String token,
            @ApiParam(value = "经度")@RequestParam("lat")String lat,
            @ApiParam(value = "纬度")@RequestParam("lng")String lng,
            @ApiParam(value = "当前地址")@RequestParam("address")String address,
            @ApiParam(required = true,value = "话题")@RequestParam("title")String title,
            @ApiParam(value = "导语")@RequestParam("description")String description

    ){
        subjectService.saveSubject(token,lng,lat,address,title,description);
        Map<String, Object> message = new HashMap<>();
        message.put(Message.RETURN_FIELD_CODE, ReturnCode.SUCCESS);
        return message;

    }
}
